<?php

namespace app\agentapi\controller;

use app\agentapi\model\Wxapp as WxappModel;
use app\common\exception\BaseException;
use app\agentapi\model\User;
use app\agentapi\model\Base as BaseModel;
use think\Cache;
use think\Request;
use think\Db;
//  指定允许其他域名访问
header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
// 响应头设置
header('Access-Control-Allow-Credentials:false');
/**
 * 控制器基类
 */
class Controller extends \think\Controller{
    /*
     * 验证登录状态
     */
    protected $sessionInfo='';
    protected $userId='';
    protected $uri=[];
    public function __construct(){
        parent::__construct();
        $userId=input('post.userId');
        $this->userId=$userId;
        $this->uri=[
            "/agentapi/order.Billds/getall",
            "/agentapi/order.Billds/insertall",
            "/agentapi/order.Billds/addredis",
            "/agentapi/order.Billds/getRedis",
            "/agentapi/order.Billds/updateredis",
            "/agentapi/order.Billds/delredis",
            "/agentapi/passport/login",
            "/agentapi/passport/updatePassword",
            "/agentapi/user.user/upload",
            "/agentapi/index/listInfo",
            "/agentapi/index/selectData",
            "/agentapi/index/selectList",
            "/agentapi/index/submit",
            "/agentapi/index/info",
            "/agentapi/index/upload",
            "/agentapi/user.user/send",
            "/agentapi/user.build/getBuildInfo",
            "/agentapi/user.user/sendkehuSms",
            "/agentapi/user.user/updatePassword",
            "/agentapi/user.user/sendMessage",
            "/agentapi/user.user/qrcode"//小程序二维码
        ];
        if(!in_array($_SERVER['PATH_INFO'],$this->uri)){
            //验证登录信息
			$token=input('post.token');
			if(empty($token)){
				throw new BaseException(['code'=>'-1','msg'=>'token信息不能为空']);
			}
			if(empty($userId)){
				throw new BaseException(['code'=>'error','msg'=>'用户id不能为空']);
			}
			$data=Cache::get($userId);
			//var_dump($data);var_dump($userId);
			if($data!=$token){
				throw new BaseException(['code'=>'-1','msg'=>'token信息不正确']);
			}
            $userInfo=Db::table('yoshop_yonghui_user')->where(['id'=>$userId])->field('password',true)->find();
			if(empty($userInfo)){
                throw new BaseException(['code'=>'-1','msg'=>'用户信息不存在']);
            }
            if($userInfo['status']==2){
                throw new BaseException(['code'=>'-1','msg'=>'该用户已被禁用']);
            }
            $companyInfo=Db::name('yonghui_company')->where(['id'=>$userInfo['companyId']])->field('id,status')->find();
            if($companyInfo['status']==2){
                throw new BaseException(['code'=>'-1','msg'=>'该用户所属公司已被禁用']);
            }
        }
    }

    /**
     * 获取post数据 (数组)
     * @param $key
     * @return mixed
     */
    protected function postData($key = null){
        return $this->request->post(is_null($key) ? '' : $key . '/a');
    }
}
